import java.util.*;

public class MysteriousRestaurant {
    public int maxDays(String[] prices, int budget) {

        this.cost = new int[prices.length][prices[0].length()];
        for (int i = 0; i < cost.length; ++i) {
            for (int j = 0; j < cost[0].length; ++j) {
                cost[i][j] = get(prices[i].charAt(j));
            }
        }
        int currentCost[] = new int[cost[0].length ];


        for (int currentDays = prices.length; currentDays >= 0; --currentDays) {
            int currentPrice = 0;
            for (int day = 0; day < 7; ++day) {
                Arrays.fill(currentCost, 0);
                for (int product = 0; product < cost[0].length; ++product) {
                    for (int index = day; index < currentDays; index += 7) {
                        currentCost[product] += cost[index][product];
                    }
                }
                int currentMin = Integer.MAX_VALUE;
                for (int current : currentCost){
                    currentMin = Math.min(currentMin, current);
                }
                currentPrice += currentMin;
            }
            if (currentPrice <= budget){
                return currentDays;
            }
        }
        return 0;
    }

    private static int get(char x) {
        if (Character.isDigit(x)) {
            return x - '0';
        } else if (Character.isLowerCase(x)) {
            return x - 'a' + 36;
        } else return x - 'A' + 10;
    }

    private int[][] cost;
}
